//
//  SmallBannerCell.m
//  Dianping
//
//  Created by 祝文博 on 15/3/11.
//  Copyright (c) 2015年 KevinLab. All rights reserved.
//

#import "SmallBannerCell.h"
#import "Banner.h"
#import "UIImageView+Additions.h"

@interface SmallBannerSubItem ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *detailLabel;

@end

@implementation SmallBannerSubItem

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, frame.size.height - 20, frame.size.height - 20)];
        self.imageView.contentMode = UIViewContentModeScaleAspectFill;
        [self addSubview:self.imageView];
        
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.backgroundColor = [UIColor clearColor];
        self.titleLabel.font = [UIFont systemFontOfSize:16];
        self.titleLabel.textColor = [UIColor colorWithHex:0x333333];
        self.titleLabel.text = @"搭配组合搭配";
        [self.titleLabel sizeToFit];
        self.titleLabel.center = CGPointMake(CGRectGetMaxX(self.imageView.frame) + 5 + self.titleLabel.bounds.size.width / 2.0, self.imageView.frame.origin.y + 5 + self.titleLabel.bounds.size.height / 2.0);
//        NSLog(@"titleLabel center : %f,%f", self.titleLabel.center.x, self.titleLabel.center.y);
        [self addSubview:self.titleLabel];
        
        
        self.detailLabel = [[UILabel alloc] init];
        self.detailLabel.backgroundColor = [UIColor clearColor];
        self.detailLabel.font = [UIFont systemFontOfSize:12];
        self.detailLabel.textColor = [UIColor blackColor];
        self.detailLabel.text = @"组合购买更实惠啊";
        [self.detailLabel sizeToFit];
        self.detailLabel.center = CGPointMake(self.titleLabel.frame.origin.x + self.detailLabel.bounds.size.width / 2.0, CGRectGetMaxY(self.titleLabel.frame) + 5 + self.detailLabel.bounds.size.height / 2.0);
//        NSLog(@"detailLabel center : %f,%f", self.detailLabel.center.x, self.detailLabel.center.y);
        [self addSubview:self.detailLabel];
    }
    
    return self;
}

@end


#pragma mark - SmallBannerCell

#define CELL_HEIGHT 65

@interface SmallBannerCell ()

@property (nonatomic, strong) UILabel *separator;

@end

@implementation SmallBannerCell

+ (CGFloat)cellHeight
{
    return CELL_HEIGHT;
}

+ (NSUInteger)numberOfItems
{
    return 2;
}

- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.subItem1 = [[SmallBannerSubItem alloc] initWithFrame:CGRectMake(0, 0, (WINDOW_WIDTH - 0.5) / 2.0, CELL_HEIGHT)];
        [self.contentView addSubview:self.subItem1];
        
        self.separator = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.subItem1.frame), 0, 0.5, CELL_HEIGHT)];
        self.separator.backgroundColor = [UIColor colorWithHex:0xc8c7cc];
        [self.contentView addSubview:self.separator];
        
        self.subItem2 = [[SmallBannerSubItem alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.separator.frame), 0, self.subItem1.bounds.size.width, self.subItem1.bounds.size.height)];
        [self.contentView addSubview:self.subItem2];
        
        self.separator.hidden = YES;
        self.subItem2.hidden = YES;
    }
    
    return self;
}

- (void)awakeFromNib
{
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

#pragma mark - Private

- (void)setBanner1:(Banner *)banner1
{
    _banner1 = banner1;
    
    self.subItem1.titleLabel.text = banner1.title;
    self.subItem1.detailLabel.text = banner1.subTitle;
    [self.subItem1.imageView setImageWithURL:[NSURL URLWithString:banner1.imageURL] placeholderImage:[UIImage imageNamed:@"img_square"] imageType:UIImageViewThumbTypeSmall];
    
    self.subItem1.enabled = banner1 != nil;
}

- (void)setBanner2:(Banner *)banner2
{
    _banner2 = banner2;
    
    self.subItem2.titleLabel.text = banner2.title;
    self.subItem2.detailLabel.text = banner2.subTitle;
    [self.subItem2.imageView setImageWithURL:[NSURL URLWithString:banner2.imageURL] placeholderImage:[UIImage imageNamed:@"img_square"] imageType:UIImageViewThumbTypeSmall];
    
    self.subItem2.enabled = banner2 != nil;
    
    if (self.subItem2.enabled == NO) {
        self.separator.hidden = YES;

    } else {
        self.separator.hidden = NO;
        self.subItem2.hidden = NO;
    }
}



@end

